/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.dusty.fs.init;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import ru.dusty.fs.filesystem.utils.FsConfig;
import ru.dusty.fs.filesystem.utils.Utils;

/**
 *
 * @author vadim
 */
public class InitFs {
    
    private static final String NAME = "init";
    
    private FsConfig config;
    
    private String[] args;
    
    public InitFs(String[] args) {
        this.args = args; 
    }
    
    public int init() {
        if (args.length < 1) {
            System.out.println(NAME + ": missing operand after \"" + NAME + "\"");
            System.out.println(NAME + ": use \"" + NAME + " root\"");
            return 1;
        }
        
        final String root = args[0];
        config = new FsConfig(root);
        
        try {
            config.loadConfig();
        } catch (FileNotFoundException ex) {
            System.out.println(NAME + ": Can not find file with the settings.");
            System.out.println(NAME + ": The default settings are loaded.");
            config.loadDefaultConfig();
        } catch (IOException ex) {
            System.out.println(NAME + ": Error reading settings file.");
            System.out.println(NAME + ": The default settings are loaded.");
            config.loadDefaultConfig();
        } catch (NumberFormatException ex) {
            System.out.println(NAME + ": Invalid file format settings.");
            System.out.println(NAME + ": The default settings are loaded.");
            config.loadDefaultConfig();
        }
        
        System.out.println(NAME + ": Initialization...");
        
        //long timeout = System.currentTimeMillis();
        
        if (config.getBlockSize() < Utils.BLOCK_MIN_SIZE) config.setBlockSize(Utils.BLOCK_MIN_SIZE);
        
        if (config.getBlockSize() * config.getBlockCount() > Utils.FS_MAX_SIZE) {
            config.setBlockCount(Utils.FS_MAX_SIZE / config.getBlockSize());
        }
        
        final byte[] byteArray = new byte[config.getBlockSize()];
        
        for (int i = 0; i < config.getBlockCount(); i++) {
            File file = new File(root + File.separator + (i+1));
            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(byteArray);
                fos.close();
            } catch (IOException ex) {
                System.out.println(NAME + ": " + file.getPath() + ": failed to create file.");
                return 2;
            }
        }
        
        System.out.println(NAME + ": Initialization completed successfully.");
        //System.out.println(NAME + ": Runtime: " + ((double) (System.currentTimeMillis() - timeout) / 1000.0));
        System.out.println(NAME + ": Created " + config.getBlockCount() + " blocks in size by " + config.getBlockSize() + " bytes.");
        
        return 0;
    }

}
